package com.eastseeway.radioproject.utils;

import android.util.Log;
import android.widget.Toast;

import com.eastseeway.radioproject.RadioApplication;
import com.eastseeway.radioproject.entities.BeanOfElectrode;
import com.google.gson.Gson;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelReader {
    public List<BeanOfElectrode> readExcel(File file) {
        if (!file.exists()) {
            Toast.makeText(RadioApplication.getAppContext(), "文件不存在", Toast.LENGTH_SHORT).show();
            return null;
        }
        List<BeanOfElectrode> dataList = new ArrayList<>();
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
//            XSSFWorkbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                int columns = row.getLastCellNum();
                BeanOfElectrode beanOfElectrode = new BeanOfElectrode();
                for (int i = 0; i < columns; i++) {
                    Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
                    if (cell != null) {
                        switch (i) {
//                            温度	功率	刺入深度	针头	能量	作用时长
                            case 0:beanOfElectrode.setTargetTemp(cell.toString());break;
                            case 1:beanOfElectrode.setPower(cell.toString());break;
                            case 2:beanOfElectrode.setDepth(cell.toString());break;
                            case 3:beanOfElectrode.setNumber(cell.toString());break;
                            case 4:beanOfElectrode.setEnergy(cell.toString());break;
                            case 5:beanOfElectrode.setDuration(cell.toString());break;
                        }
                    }
                }
                dataList.add(beanOfElectrode);
            }
            fileInputStream.close();
        } catch (Exception e) {
            Log.e("12121","Exception: "+e.getMessage());
            e.printStackTrace();
        }finally {
        }
        return dataList;
    }
}
